import axios from 'axios';
import qs from 'qs';
import { apiUrl, hncWebUrl, cloudUrl, hncWebAdminUrl, flowUrl, stationUrl } from '@/api/URLConfig';

axios.defaults.timeout = 100000;

// 添加请求拦截器: 让post请求的请求体格式为urlencoded格式 a=1&b=2
// 在真正发请求前执行

// axios.interceptors.request.use(function(config) {
//   // 得到请求方式和请求体数据
//   const { method, data, headers } = config;
//   let hasContentType = headers['Content-Type'];
//   let isHeadersOfJson = headers['Content-Type'] && headers['Content-Type'] == 'application/json';
//   let isHeadersOfFormData = headers['Content-Type'] && headers['Content-Type'] == 'multipart/form-data';
//   let isHeadersOfUrlencoded = headers['Content-Type'] && headers['Content-Type'] == 'application/x-www-form-urlencoded';
//   let isObject = typeof data === 'object';
//   let isFormData = data instanceof FormData;
//   if (method.toLowerCase() === 'post') {
//     if (isHeadersOfJson) {
//     } else if (isHeadersOfFormData) {
//       console.log('isHeadersOfFormData');
//     } else if (isHeadersOfUrlencoded) {
//       config.data = qs.stringify(data);
//     } else {
//     }
//   }
//   return config;
// });

const cloudManagerGet = (url, params = {}) => {
  return new Promise((resolve, reject) => {
    axios
      .get(apiUrl + url, {
        params: params,
      })
      .then(
        (response) => {
          resolve(response.data);
        },
        (err) => {
          reject(err);
        }
      )
      .catch((error) => {
        reject(error);
      });
  });
};

// 针对云掌柜后面的页面；
const cloudManagerPost = (url, params = {}, config) => {
  return new Promise((resolve, reject) => {
    axios
      .post(apiUrl + url, params, config)
      .then(
        (response) => {
          resolve(response.data);
        },
        (err) => {
          reject(err);
        }
      )
      .catch((error) => {
        reject(error);
      });
  });
};

const hncWebGet = (url, params = {}) => {
  return new Promise((resolve, reject) => {
    axios
      .get(hncWebUrl + url, {
        params: params,
      })
      .then(
        (response) => {
          resolve(response.data);
        },
        (err) => {
          reject(err);
        }
      )
      .catch((error) => {
        reject(error);
      });
  });
};

// 针对云掌柜后面的hncWeb页面；
const hncWebPost = (url, params = {}, config) => {
  return new Promise((resolve, reject) => {
    axios
      .post(hncWebUrl + url, params, config)
      .then(
        (response) => {
          resolve(response.data);
        },
        (err) => {
          reject(err);
        }
      )
      .catch((error) => {
        reject(error);
      });
  });
};
const hnccloudGet = (url, params = {}) => {
  return new Promise((resolve, reject) => {
    axios
      .get(cloudUrl + url, {
        params: params,
      })
      .then(
        (response) => {
          resolve(response.data);
        },
        (err) => {
          reject(err);
        }
      )
      .catch((error) => {
        reject(error);
      });
  });
};

// 针对云掌柜后面的hncWeb页面；
const hnccloudPost = (url, params = {}, config) => {
  return new Promise((resolve, reject) => {
    axios
      .post(cloudUrl + url, params, config)
      .then(
        (response) => {
          resolve(response.data);
        },
        (err) => {
          reject(err);
        }
      )
      .catch((error) => {
        reject(error);
      });
  });
};

const hncWebAdminGet = (url, params = {}) => {
  return new Promise((resolve, reject) => {
    axios
      .get(hncWebAdminUrl + url, {
        params: params,
      })
      .then(
        (response) => {
          resolve(response.data);
        },
        (err) => {
          reject(err);
        }
      )
      .catch((error) => {
        reject(error);
      });
  });
};

const hncWebAdminPost = (url, params = {}, config) => {
  return new Promise((resolve, reject) => {
    axios
      .post(hncWebAdminUrl + url, params, config)
      .then(
        (response) => {
          resolve(response.data);
        },
        (err) => {
          reject(err);
        }
      )
      .catch((error) => {
        reject(error);
      });
  });
};

// 流量接口
const flowUrlGet = (url, params = {}) => {
  return new Promise((resolve, reject) => {
    axios
      .get(flowUrl + url, {
        params: params,
      })
      .then(
        (response) => {
          resolve(response.data);
        },
        (err) => {
          reject(err);
        }
      )
      .catch((error) => {
        reject(error);
      });
  });
};

const flowUrlPost = (url, params = {}, config) => {
  return new Promise((resolve, reject) => {
    axios
      .post(flowUrl + url, params, config)
      .then(
        (response) => {
          resolve(response.data);
        },
        (err) => {
          reject(err);
        }
      )
      .catch((error) => {
        reject(error);
      });
  });
};

// 流量接口
const stationUrlGet = (url, params = {}) => {
  return new Promise((resolve, reject) => {
    axios
      .get(stationUrl + url, {
        params: params,
      })
      .then(
        (response) => {
          resolve(response.data);
        },
        (err) => {
          reject(err);
        }
      )
      .catch((error) => {
        reject(error);
      });
  });
};

const stationUrlPost = (url, params = {}, config) => {
  return new Promise((resolve, reject) => {
    axios
      .post(stationUrl + url, params, config)
      .then(
        (response) => {
          resolve(response.data);
        },
        (err) => {
          reject(err);
        }
      )
      .catch((error) => {
        reject(error);
      });
  });
};

export default {
  cloudManagerGet,
  cloudManagerPost,
  hncWebGet,
  hncWebPost,
  hnccloudGet,
  hnccloudPost,
  hncWebAdminGet,
  hncWebAdminPost,
  flowUrlGet,
  flowUrlPost,
  stationUrlGet,
  stationUrlPost,
};
